<template>
  <el-dialog
    :title="!dataForm.clinicalId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="日期" prop="clinicalDate">
      <el-input v-model="dataForm.clinicalDate" placeholder="日期"></el-input>
    </el-form-item>
    <el-form-item label="金额" prop="clinicalPrice">
      <el-input v-model="dataForm.clinicalPrice" placeholder="金额"></el-input>
    </el-form-item>
    <el-form-item label="患者id" prop="patientId">
      <el-input v-model="dataForm.patientId" placeholder="患者id"></el-input>
    </el-form-item>
    <el-form-item label="消息" prop="clinicalMsg">
      <el-input v-model="dataForm.clinicalMsg" placeholder="消息"></el-input>
    </el-form-item>
    <el-form-item label="医生id" prop="docterId">
      <el-input v-model="dataForm.docterId" placeholder="医生id"></el-input>
    </el-form-item>
    <el-form-item label="接诊状态" prop="clinicalStatus">
      <el-input v-model="dataForm.clinicalStatus" placeholder="接诊状态"></el-input>
    </el-form-item>
    <el-form-item label="问诊状态" prop="clinicalState">
      <el-input v-model="dataForm.clinicalState" placeholder="问诊状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          clinicalId: 0,
          clinicalDate: '',
          clinicalPrice: '',
          patientId: '',
          clinicalMsg: '',
          docterId: '',
          clinicalStatus: '',
          clinicalState: ''
        },
        dataRule: {
          clinicalDate: [
            { required: true, message: '日期不能为空', trigger: 'blur' }
          ],
          clinicalPrice: [
            { required: true, message: '金额不能为空', trigger: 'blur' }
          ],
          patientId: [
            { required: true, message: '患者id不能为空', trigger: 'blur' }
          ],
          clinicalMsg: [
            { required: true, message: '消息不能为空', trigger: 'blur' }
          ],
          docterId: [
            { required: true, message: '医生id不能为空', trigger: 'blur' }
          ],
          clinicalStatus: [
            { required: true, message: '接诊状态不能为空', trigger: 'blur' }
          ],
          clinicalState: [
            { required: true, message: '问诊状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.clinicalId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.clinicalId) {
            this.$http({
              url: this.$http.adornUrl(`/live_streaming/clinical/info/${this.dataForm.clinicalId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.clinicalDate = data.clinical.clinicalDate
                this.dataForm.clinicalPrice = data.clinical.clinicalPrice
                this.dataForm.patientId = data.clinical.patientId
                this.dataForm.clinicalMsg = data.clinical.clinicalMsg
                this.dataForm.docterId = data.clinical.docterId
                this.dataForm.clinicalStatus = data.clinical.clinicalStatus
                this.dataForm.clinicalState = data.clinical.clinicalState
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/live_streaming/clinical/${!this.dataForm.clinicalId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'clinicalId': this.dataForm.clinicalId || undefined,
                'clinicalDate': this.dataForm.clinicalDate,
                'clinicalPrice': this.dataForm.clinicalPrice,
                'patientId': this.dataForm.patientId,
                'clinicalMsg': this.dataForm.clinicalMsg,
                'docterId': this.dataForm.docterId,
                'clinicalStatus': this.dataForm.clinicalStatus,
                'clinicalState': this.dataForm.clinicalState
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
